<%@ include file="import_page.jsp" %>
<%
	boolean loginFail = false;
	if (request.getMethod() == "POST") {

		String loginName = request.getParameter(PAR_LOGIN_NAME);
		String password = request.getParameter(PAR_PASSWORD);
		//post back
		if (!(loginName == null || loginName.isEmpty()
				|| password == null || password.isEmpty())) {
			//login process
			UserControl uCon = new UserControl();
			loginFail = !uCon.login(loginName, password);
			//close the database connection
			uCon.closeDBConnection();
			
			//If login success
			if(!loginFail) {
				//login success
				Users loginUser = uCon.getLoginUser();
				session.setAttribute(SESSION_LOGIN_USER, loginUser);
				String redirURL;
				switch(loginUser.getUserType())
				{
					case admin:
						redirURL = URL_ADMIN;
						break;
					case staff:
						redirURL = URL_STAFF;
						break;
					default:
						redirURL = URL_STUDENT;
				}
				//send Redirect
				response.sendRedirect(redirURL);
				return; //end of this page, if login success
			}
		}
		else {
			//login fail
			//mark login fail for print error message
			loginFail = true;
			session.invalidate();
		}
		
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Login</title>
<style type="text/css">
<!--
.style1 {
	font-size: large;
	font-weight: bold;
}

body {
	background-image: url(images/UniLearnSystem_bg.jpg);
}
-->
</style>
<script type="text/javascript">
function submitform()
{
	document.getElementById("login_form").submit();
}
</script>
</head>

<body>
	<form id="login_form" action="f_login.jsp" method="post">
		<table width="100%" height="125" border="1" bordercolor="#CCCCCC"
			bgcolor="#00FF99">
			<tr>
				<td height="63" colspan="2"><div align="center">
						<span class="style1">Student / Staff Login </span>
					</div>
				</td>
			</tr>
			<tr>
				<td width="53" height="26"><strong>Name:</strong>
				</td>
				<td width="296">
						<label> <input name="login_name" type="text" id="login_name" /> </label>
				</td>
			</tr>
			<tr>
				<td height="26"><strong>Password: </strong>
				</td>
				<td>
						<label> <input name="password" type="password" id="password" /> </label>
				</td>
			</tr>
<%
	if(loginFail) {
%>
		<tr>
			<td colspan="2"><div align="center"><%=ERR_LOGIN_FAIL %></div></td>
		</tr>
<%} %>
		</table>
	</form>
	<p align="center">
		<a href="javascript: submitform();"><img src="images/button_login.gif" alt="login" width="323" height="112" border="0" />
		</a>
	</p>
	<p align="center">
		<a href="Student/Student_index.html">Student</a>
	</p>
	<p align="center">
		<a href="Staff/Staff_index.html">Staff</a>
	</p>
	<p align="center">
		<a href="Admin/admin_index.html">Admin</a>
	</p>
</body>
</html>
